﻿using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Shell;

namespace Del1C
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private Uninstaller1C InstalVer = new Uninstaller1C();
        
        public MainWindow()
        {
            InitializeComponent();

            Binding binding = new Binding();

            binding.Source = InstalVer;
            list.SetBinding(ListView.ItemsSourceProperty, binding);
        }

        private async void btnGet_Click(object sender, RoutedEventArgs e)
        {
            this.TaskbarItemInfo.ProgressValue = 0d;
            this.TaskbarItemInfo.ProgressState = TaskbarItemProgressState.None;

            btnGet.IsEnabled = false;
            var temp = btnGet.Content;
            btnGet.Content = "Wait...";

            list.IsEnabled = false;
            btnUninstall.IsEnabled = false;

            await InstalVer.GetVersions();
            
            btnGet.IsEnabled = true;
            btnGet.Content = temp;

            list.IsEnabled = true;
            btnUninstall.IsEnabled = true;
        }

        private async void btnUninstall_Click(object sender, RoutedEventArgs e)
        {
            list.IsEnabled = false;
            btnGet.IsEnabled = false;
            btnUninstall.IsEnabled = false;
            btnUninstall.Content = "Uninstalling...";

            List<InstalledVersion1C> uninst = new List<InstalledVersion1C>();
            foreach (var item in InstalVer)
            {
                if (item.IsChecked && item.State == States.Installed)
                {
                    uninst.Add(item);
                }
            }

            int ind = 0;
            this.TaskbarItemInfo.ProgressValue = 0d;
            this.TaskbarItemInfo.ProgressState = TaskbarItemProgressState.Normal;

            foreach (var item in uninst)
            {
                if (item.IsChecked && item.State == States.Installed)
                {
                    item.State = States.Uninstalling;
                    this.TaskbarItemInfo.Description = item.Name + " (версия: " + item.Version + ")";

                    var result = await item.Uninstall();
                    if (result)
                    {
                        InstalVer.Remove(item);
                        ind++;
                        this.TaskbarItemInfo.ProgressValue = (double)ind / (double)uninst.Count;
                    }
                }
            }

            list.IsEnabled = true;
            btnGet.IsEnabled = true;
            btnUninstall.IsEnabled = true;
            btnUninstall.Content = "Uninstall";
            this.TaskbarItemInfo.ProgressState = TaskbarItemProgressState.None;
        }

        private async void Window_Loaded_1(object sender, RoutedEventArgs e)
        {
            list.Visibility = Visibility.Hidden;
            btnUninstall.IsEnabled = false;

            await InstalVer.GetVersions();

            list.Visibility = Visibility.Visible;
            btnUninstall.IsEnabled = true;
        }

        private async void Button_Click_1(object sender, RoutedEventArgs e)
        {
            //var ind = (int)(sender as Button).Tag;

            //var item = InstalVer[ind];
            //item.State = States.Uninstalling;
            //await item.Uninstall();
            //InstalVer.Remove(item);

            var item = FindVisualParent<ListItem>(sender as Button);
        }

        public static T FindVisualParent<T>(DependencyObject childElement) where T : DependencyObject
        {
            DependencyObject parent = VisualTreeHelper.GetParent(childElement);
            T parentAsT = parent as T;
            if (parent == null)
            {
                return null;
            }
            else if (parentAsT != null)
            {
                return parentAsT;
            }
            return FindVisualParent<T>(parent);
        }
    }
}
